package com.example.demo.service.impl;

import com.example.demo.entity.EconomicIndicators;
import com.example.demo.entity.GeneralPublicBudgetRevenue;
import com.example.demo.entity.MinEconomicIndicators;
import com.example.demo.entity.QuarterlyGrossRegionalProduct;
import com.example.demo.mapper.EconomicIndicatorsMapper;
import com.example.demo.service.EconomicIndicatorsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@Service
public class EconomicIndicatorsServiceImpl implements EconomicIndicatorsService {

    @Autowired
    EconomicIndicatorsMapper economicIndicatorsMapper;


    /**
     *季度地区生产总值
     * @param year 年份（int）
     *@return List<QuarterlyGrossRegionalProduct>
     */
    @Override
    public List<QuarterlyGrossRegionalProduct> getQuarterlyGrossRegionalProduct(int year) {
        double quarterValueList[] = new double[5];
        double quarterOnQuarterList[] = new double[4];
        List<EconomicIndicators> list1 = economicIndicatorsMapper.findQuarterlyCumulativeProduction(year);
        List<QuarterlyGrossRegionalProduct> list2 = new ArrayList<>();
        Iterator<EconomicIndicators> it = list1.iterator();
        while (it.hasNext()) {
            EconomicIndicators economicIndicators = it.next();
            if (economicIndicators.getMonth() == 1 | economicIndicators.getMonth() == 2 | economicIndicators.getMonth() == 3) {
                quarterValueList[1] += economicIndicators.getThisMonth();
            }
            if (economicIndicators.getMonth() == 4 | economicIndicators.getMonth() == 5 | economicIndicators.getMonth() == 6) {
                quarterValueList[2] += economicIndicators.getThisMonth();
            }
            if (economicIndicators.getMonth() == 7 | economicIndicators.getMonth() == 8 | economicIndicators.getMonth() == 9) {
                quarterValueList[3] += economicIndicators.getThisMonth();
            }
            if (economicIndicators.getMonth() == 10 | economicIndicators.getMonth() == 11 | economicIndicators.getMonth() == 12) {
                if(economicIndicators.getYear() == year){
                    quarterValueList[4] += economicIndicators.getThisMonth();
                }
                if(economicIndicators.getYear() == year){
                    quarterValueList[0] += economicIndicators.getThisMonth();
                }
            }
        }

        for(int i = 1;i <= 4;i++){
            if(quarterValueList[i - 1] == 0){
                quarterOnQuarterList[i - 1] = 0;
            }
            else{
                double quarterOnQuarter = ((quarterValueList[i] - quarterValueList[i - 1])/quarterValueList[i - 1])*100;
                double value = new BigDecimal(quarterOnQuarter).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                quarterOnQuarterList[i - 1] = value;
            }
            QuarterlyGrossRegionalProduct quarterlyCumulativeProduction = new QuarterlyGrossRegionalProduct(i,quarterValueList[i],quarterOnQuarterList[i - 1]);
            list2.add(quarterlyCumulativeProduction);
        }

        return list2;
    }

    /**
     *季度地区累计生产总值
     * @param year 年份（int）
     *@return List<QuarterlyGrossRegionalProduct>
     */
    @Override
    public List<QuarterlyGrossRegionalProduct> getAccumulatedQuarterlyGrossRegionalProduct(int year) {
        List<QuarterlyGrossRegionalProduct> list = this.getQuarterlyGrossRegionalProduct(year);
        double total = 0;
        double tmp = 0;
        Iterator<QuarterlyGrossRegionalProduct> it = list.iterator();
        while (it.hasNext()) {
            QuarterlyGrossRegionalProduct quarterlyGrossRegionalProduct = it.next();
            tmp = quarterlyGrossRegionalProduct.getQuarterValue();
            quarterlyGrossRegionalProduct.setQuarterValue(tmp + total);
            total += tmp;
        }
        return list;
    }

    /**
     * 年度任务及单位
     * @param year 年份（int），indicators 指标 （String）
     *@return EconomicIndicators
     */
    @Override
    public EconomicIndicators getAnnualTasks(int year,String indicators) {
        return economicIndicatorsMapper.findAnnualTasks(year, indicators);
    }

    /**
     * 一般公共预算收入
     * @param year 年份（int）
     *@return List<GeneralPublicBudgetRevenue>
     */
    @Override
    public List<GeneralPublicBudgetRevenue> getGeneralPublicBudgetRevenue(int year) {
        return economicIndicatorsMapper.findGeneralPublicBudgetRevenue(year);
    }

    /**
     * 全社会固定资产投资
     * 工业投资
     * 规上工业总产值
     * 社会消费品零售总额
     * 进出口总额
     * 出口总额
     * 实际使用外资
     * @param year 年份（int）,indicators 指标 (String)
     * @return List<MinEconomicIndicators>
     */
    @Override
    public List<MinEconomicIndicators> getEconomicIndicatorsByIndicators(int year, String indicators) {
//        if(indicators == "出口总额" || indicators == "工业投资"){
//            indicators = "#" + indicators;
//        }
        return economicIndicatorsMapper.findEconomicIndicatorsByIndicators(year, indicators);
    }
}
